package com.inei.pruebamapas;

import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;

public class MapaActivity extends FragmentActivity implements LocationListener {

	GoogleMap mapa;
	Marker marker;
	Circle precision;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mapa);
		mapa = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();
		//mapa.setMyLocationEnabled(true);
		int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
		
		int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
		if (resultCode != ConnectionResult.SUCCESS) {
		   if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
		       GooglePlayServicesUtil.getErrorDialog(resultCode, this,
		    		   PLAY_SERVICES_RESOLUTION_REQUEST).show();
		   } else {
		       Log.i("MAPA!!!!", "This device is not supported.");
		       finish();
		   }
		}
		/*
		// mapa.setMyLocationEnabled(true);
		LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000,
				1, this);
		setUpMapIfNeeded();
		agregarPoligono();*/
	}

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setUpMapIfNeeded();
	}
	private void setUpMapIfNeeded() {
        if (mapa == null) {            
        	mapa = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        }
    }
	public void agregarPoligono(){
		
		mapa.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-12.993982609913758,-74.077073483050356),15));
		PolygonOptions options = new PolygonOptions();
		options.strokeWidth(1);
		options.add(new LatLng(-12.993982609913758,-74.0770734830503),new LatLng(-12.993982229999972,-74.0770743589999),new LatLng(-12.99314648099994,-74.0774453579999),new LatLng(-12.992137729999968,-74.0775977329998),new LatLng(-12.99042710599997,-74.0775977339999),new LatLng(-12.988306104999936,-74.0772898589998),new LatLng(-12.986390230999916,-74.0770162329999),new LatLng(-12.98550060599996,-74.0772214839999),new LatLng(-12.984946104999892,-74.0773809839999),new LatLng(-12.984645480999973,-74.0779741089999),new LatLng(-12.984371730999896,-74.0793083579998),new LatLng(-12.984029605999922,-74.0805057339999),new LatLng(-12.983824480999829,-74.0813609839999),new LatLng(-12.983419904883021,-74.0825407195819),new LatLng(-12.983432174999791,-74.0825642289999),new LatLng(-12.98348519599989,-74.0826495129999),new LatLng(-12.984871910648906,-74.0823640749072),new LatLng(-12.985262105999936,-74.0822544489999),new LatLng(-12.985624199999791,-74.0822275639999),new LatLng(-12.986089730999936,-74.0824661709998),new LatLng(-12.98653008999986,-74.0829890139998),new LatLng(-12.987175496999896,-74.0844196799999),new LatLng(-12.988103618798675,-74.0859732714894),new LatLng(-12.988494679175119,-74.0863934043387),new LatLng(-12.988640873999884,-74.0865302899999),new LatLng(-12.9888886718864,-74.0863571713815),new LatLng(-12.990007774999924,-74.0850995559998),new LatLng(-12.990530680999939,-74.0840685429999),new LatLng(-12.990538832999903,-74.0837584709999),new LatLng(-12.990865567999947,-74.0832305909998),new LatLng(-12.991046053999867,-74.0823409489999),new LatLng(-12.991813836999881,-74.0819871579999),new LatLng(-12.992531220999922,-74.0815841369998),new LatLng(-12.99305245362649,-74.0816206230513),new LatLng(-12.994290655999919,-74.0822673199999),new LatLng(-12.994983540999954,-74.0819925869999),new LatLng(-12.995518854999943,-74.0815885669998),new LatLng(-12.996117013999935,-74.0807643399999),new LatLng(-12.996636504999856,-74.0801178049998),new LatLng(-12.996856819999948,-74.0794550629998),new LatLng(-12.996998412999858,-74.0782747439998),new LatLng(-12.996746079999866,-74.0775155579998),new LatLng(-12.99554116920001,-74.0769298710166),new LatLng(-12.99523364199996,-74.0768717339999),new LatLng(-12.994840071999818,-74.0770980729999),new LatLng(-12.994020057999933,-74.0770881169998),new LatLng(-12.9939826099137,-74.0770734830503));
		options.fillColor(Color.HSVToColor(50, new float[]{25,1,1}));
		Polygon p=  mapa.addPolygon(options);
		LatLng pos = new LatLng(-12.993982609913758,-74.0770734830503);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.mapa, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location loc) {
		// TODO Auto-generated method stub
		if (marker == null) {
			LatLng pos = new LatLng(loc.getLatitude(), loc.getLongitude());
			
			MarkerOptions options = new MarkerOptions();
			options.icon(BitmapDescriptorFactory
					.fromResource(R.drawable.ic_launcher));
			options.position(pos);
			//mapa.animateCamera(CameraUpdateFactory.newLatLngZoom(pos,15));
			marker = mapa.addMarker(options);
			CircleOptions cOpt = new CircleOptions();
			cOpt.center(pos);
			cOpt.radius(loc.getAccuracy());
			cOpt.fillColor(Color.HSVToColor(50, new float[]{25,1,1}));
			precision = mapa.addCircle(cOpt);
			
			Log.i("Prueba", "marker creado!");
			Log.i("Prueba", "accuracy = "+loc.getAccuracy());
			Log.i("Prueba", "latitud = " + loc.getLatitude());
			Log.i("Prueba", "longitud = " + loc.getLongitude());
		} else {
			LatLng pos = new LatLng(loc.getLatitude(), loc.getLongitude());
			precision.setCenter(pos);
			precision.setRadius(loc.getAccuracy());
			
			Log.i("Prueba", "cambio ubicacion");
			Log.i("Prueba", "accuracy = "+loc.getAccuracy());
			Log.i("Prueba", "latitud = " + loc.getLatitude());
			Log.i("Prueba", "longitud = " + loc.getLongitude());
			marker.setPosition(pos);
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
